package com.shxy.service.impl;

import com.shxy.controller.ui.Pageable;
import com.shxy.dao.base.ClazzDao;
import com.shxy.dao.base.CollegeDao;
import com.shxy.dao.base.MajorDao;
import com.shxy.dao.base.StudentDao;
import com.shxy.entity.base.College;
import com.shxy.entity.base.Major;
import com.shxy.entity.custom.CustomClazz;
import com.shxy.entity.custom.CustomStudent;
import com.shxy.service.IQueryService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
@Log
public class QueryService implements IQueryService {
    @Autowired
    private StudentDao studentDao;
    @Autowired private CollegeDao collegeDao;
    @Autowired private MajorDao majorDao;
    @Autowired private ClazzDao clazzDao;

    @Override
    public List<College> getCollegesAll() {
        return collegeDao.findAll();
    }

    @Override
    public List<Major> getMajorsAll() {
        return majorDao.findAll();
    }

    @Override
    public List<Major> getMajorsByCollegeAndYear(int collegeId, String year) {
        List<Major> majors = clazzDao.findByYear(year).stream().filter(customClazz -> {
            return customClazz.getCollege().getId() == collegeId;
        }).map(CustomClazz::getMajor).collect(Collectors.toList());
        return majors;
    }

    @Override
    public List<CustomClazz> getClazzsByMajorAndYear(int majorId, String year) {
        List<CustomClazz> clazzes = clazzDao.findByYear(year).stream().filter(customClazz -> {
            return customClazz.getMajor().getId() == majorId;
        }).collect(Collectors.toList());
        return clazzes;
    }

    @Override
    public List<CustomStudent> getStusByClazz(int clazzId) {
        return studentDao.findByClazzId(clazzId);
    }

    @Override
    public Pageable queryStusList(String year, int collegeId, int majorId, int clazzId, int page, int size) {
        // 0、如果没有传入年份，则获取当前年份的学生
        if(year.isBlank()) {
            year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        }
        // 1、按学年查询学生列表
        List<CustomStudent> stus = studentDao.findAllByClazzYear(year);

        // 2、创建过滤条件
        Predicate<CustomStudent> predicate = null;

        // 3、计算过滤条件
        if(clazzId != 0) {
            predicate = (cs) -> cs.getClazz().getId() == clazzId;
        }
        else if(majorId != 0) {
            predicate = (cs) -> cs.getClazz().getMajor().getId() == majorId;
        }
        else if(collegeId != 0) {
            predicate = (cs) -> cs.getClazz().getCollege().getId() == collegeId;
        }
        if(predicate == null) {
            predicate = (cs) -> true;
        }

        // 4、过滤
        List<CustomStudent> students = stus.stream().filter(predicate).collect(Collectors.toList());
        // 5、分页
        List<CustomStudent> stuList = students.stream().skip((page - 1) * size).limit(size).collect(Collectors.toList());

        return new Pageable<CustomStudent>(students.size(), stuList, page, size);
    }

    @Override
    public Optional<CustomStudent> getStuByCode(String code) {
        return studentDao.findByCode(code);
    }

    @Override
    public List<CustomClazz> getClazzsAll() {
        return clazzDao.findAll();
    }

    @Override
    public List<CustomStudent> getStusByYear(String year) {
        return studentDao.findAllByClazzYear(year);
    }
}
